package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.PageResult;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import cn.outsourcing.supermarket.common.utils.IPageUtils;
import cn.outsourcing.supermarket.common.utils.PageResultUtils;
import cn.outsourcing.supermarket.common.utils.StrUtils;
import cn.outsourcing.supermarket.converter.HiolabsCartConverter;
import cn.outsourcing.supermarket.converter.HiolabsUserConverter;
import cn.outsourcing.supermarket.dto.backstage.BackstageShopCartDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageUserListDTO;
import cn.outsourcing.supermarket.mapper.BackstageHomeMapper;
import cn.outsourcing.supermarket.mapper.HiolabsCartMapper;
import cn.outsourcing.supermarket.mapper.HiolabsUserMapper;
import cn.outsourcing.supermarket.po.HiolabsCart;
import cn.outsourcing.supermarket.po.HiolabsUser;
import cn.outsourcing.supermarket.service.BackstageHomeService;
import cn.outsourcing.supermarket.vo.backstage.BackstageUserListVO;
import cn.outsourcing.supermarket.vo.backstage.MainInfoVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsCartVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 后台服务接口实现类
 *
 * @author gzkemays
 * @since 2021/10/20 17:40
 */
@Service
public class BackstageHomeServiceImpl implements BackstageHomeService {
  @Resource BackstageHomeMapper backstageHomeMapper;
  @Resource HiolabsUserMapper userMapper;
  @Resource HiolabsCartMapper cartMapper;

  @Override
  public Map<String, Long> getBaseData() {
    return backstageHomeMapper.getHomeBasicData();
  }

  @Override
  public MainInfoVO getMainData(Integer pindex) {
    long time;
    long todayZero = DataUtils.getTodayTimestampZero();
    switch (pindex) {
      case 1:
        time = DataUtils.getBeforeTimestampZero(-1);
        break;
      case 2:
        time = DataUtils.getBeforeTimestampZero(-7);
        break;
      case 3:
        time = DataUtils.getBeforeTimestampZero(-30);
        break;
      default:
        time = todayZero;
    }
    List<HiolabsUser> users = userMapper.selectList(null);
    List<HiolabsUser> newRegUser =
        users.stream()
            .filter(user -> user.getRegisterTime().compareTo((int) time) > -1)
            .collect(Collectors.toList());
    List<HiolabsUser> oldUser =
        users.stream()
            .filter(
                user -> {
                  Integer registerTime = user.getRegisterTime();
                  Integer lastLoginTime = user.getLastLoginTime();
                  return registerTime.compareTo((int) time) < 1
                      && lastLoginTime.compareTo((int) todayZero) > -1;
                })
            .collect(Collectors.toList());
    MainInfoVO detailedList = backstageHomeMapper.getDetailedList(time);
    detailedList.setNewData(HiolabsUserConverter.INSTANCE.convertToVoList(newRegUser));
    detailedList.setOldData(HiolabsUserConverter.INSTANCE.convertToVoList(oldUser));
    detailedList.setNewUser(newRegUser.size());
    detailedList.setOldUser(oldUser.size());
    return detailedList;
  }

  @Override
  public PageResult<HiolabsCartVO> getCartShop(BackstageShopCartDTO dto) {
    IPageUtils<HiolabsCart> properties = IPageUtils.<HiolabsCart>init().properties(dto);
    List<HiolabsCart> records =
        cartMapper
            .selectPage(
                properties,
                Wrappers.<HiolabsCart>lambdaQuery()
                    .like(HiolabsCart::getGoodsName, dto.getName().trim())
                    .orderByDesc(HiolabsCart::getAddTime))
            .getRecords();
    return PageResultUtils.parseListPageObjs(
        properties, HiolabsCartConverter.INSTANCE.convertToVoList(records));
  }

  @Override
  public Map<String, PageResult<BackstageUserListVO>> getUserList(BackstageUserListDTO dto) {
    IPageUtils<HiolabsUser> properties = IPageUtils.<HiolabsUser>init().properties(dto);
    String nickname = Optional.ofNullable(dto.getNickname()).orElse("");
    List<HiolabsUser> records =
        userMapper
            .selectPage(
                properties,
                Wrappers.<HiolabsUser>lambdaQuery()
                    .like(HiolabsUser::getNickname, StrUtils.getBase64Str(nickname))
                    .orderByDesc(HiolabsUser::getRegisterTime))
            .getRecords();
    Map<String, PageResult<BackstageUserListVO>> map = new HashMap<>(1);
    map.put(
        "userData",
        PageResultUtils.parseListPageObjs(
            properties, HiolabsUserConverter.INSTANCE.convertToBackstageUserVoList(records)));
    return map;
  }
}
